﻿Imports DTO
Imports BUS

Public Class XepLop
    Public bangHS As New DataTable
    Dim dataBus As New DataProviderBUS

    Private Sub XepLop_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim BangLH As New DataTable
        Dim LopHoc As New LopHocBUS
        Dim HocSinh As New HocSinhBUS

        BangLH = LopHoc.LayDSLH()
        Me.cmbLopHoc.DataSource = BangLH
        Me.cmbLopHoc.DisplayMember = "TenLH"
        Me.cmbLopHoc.ValueMember = "MaLH"

        Dim LHChuaXep As New DataTable
        LHChuaXep = HocSinh.DSHSChuaXep()
        Me.gridDSHocSinh.DataSource = LHChuaXep

        bangHS = dataBus.Doc_Cau_Truc("HocSinh")

    End Sub

    Private Sub cmbLopHoc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbLopHoc.SelectedIndexChanged
        Try
            cmbLopHoc.ValueMember = "MaLH"
            Dim LopHoc As New LopHocBUS

            Dim maLH As Integer
            maLH = Me.cmbLopHoc.SelectedValue

            Dim siSo As Integer
            siSo = LopHoc.LaySiSo(maLH)

            Me.txtSiSo.Text = siSo.ToString()

            Dim qdBUS As New QuyDinhBUS
            Dim SiSoMax As Integer
            SiSoMax = qdBUS.LaySiSoToiDa()

            If (Integer.Parse(Me.txtSiSo.Text) <= SiSoMax) Then
                Me.txtSiSo.BackColor = Color.Aqua
            Else
                Me.txtSiSo.BackColor = Color.Red
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub gridDSHocSinh_CellMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles gridDSHocSinh.CellMouseClick
        Try
            If (Me.gridDSHocSinh.SelectedRows.Count > 0) Then
                Dim hs As DataRow
                hs = bangHS.NewRow
                Dim s1 As String
                Dim s2 As String
                Dim s3 As String
                Dim s4 As String
                Dim s5 As String
                Dim s6 As String
                hs("HoTen") = Me.gridDSHocSinh.CurrentRow.Cells(2).Value
                hs("NgaySinh") = Me.gridDSHocSinh.CurrentRow.Cells(3).Value
                hs("DiaChi") = Me.gridDSHocSinh.CurrentRow.Cells(4).Value
                hs("Email") = Me.gridDSHocSinh.CurrentRow.Cells(5).Value
                hs("GioiTinh") = Me.gridDSHocSinh.CurrentRow.Cells(6).Value
                hs("MaHS") = Me.gridDSHocSinh.CurrentRow.Cells(1).Value
                s1 = Me.gridDSHocSinh.CurrentRow.Cells(1).Value
                s2 = Me.gridDSHocSinh.CurrentRow.Cells(2).Value
                s3 = Me.gridDSHocSinh.CurrentRow.Cells(3).Value
                s4 = Me.gridDSHocSinh.CurrentRow.Cells(4).Value
                s5 = Me.gridDSHocSinh.CurrentRow.Cells(5).Value
                s6 = Me.gridDSHocSinh.CurrentRow.Cells(6).Value

                bangHS.Rows.Add(hs)
                Me.gridXepLop.DataSource = bangHS
                Dim i As Integer
                For i = 0 To gridXepLop.Rows.Count - 2
                    Me.gridXepLop.Rows(i).Cells(0).Value = i + 1

                Next

            End If
        Catch ex As Exception
            MsgBox("Học Sinh này đã được chọn")
        End Try

    End Sub

    Private Sub btnXepLop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXepLop.Click
        Try
            If (Me.gridDSHocSinh.Rows.Count = 0) Then
                MsgBox("Ko co hs moi", MsgBoxStyle.Exclamation, "warning")
                Exit Sub

            End If

            If (Me.gridDSHocSinh.Focused = False) Then
                MsgBox("chua chon hs")
                Exit Sub
            End If
            Dim qdBUS As New QuyDinhBUS
            Dim SiSoMax As Integer
            SiSoMax = qdBUS.LaySiSoToiDa()

            Dim soDong As Integer
            soDong = Me.gridXepLop.Rows.Count - 1

            Dim siSo As Integer
            siSo = Integer.Parse(Me.txtSiSo.Text) + soDong

            If (siSo > SiSoMax) Then
                MsgBox("vượt quá sĩ số", MsgBoxStyle.Critical)

            Else
                Dim hsBus As New HocSinhBUS
                Dim maHS As Integer
                Dim maLH As Integer
                maLH = Integer.Parse(Me.cmbLopHoc.SelectedValue)
                Dim i As Integer
                For i = 0 To Me.gridXepLop.Rows.Count - 2
                    maHS = Me.gridXepLop.Rows(i).Cells(1).Value
                    hsBus.CapNhat(maHS, maLH)
                Next

                MsgBox("Xếp lớp cho HS thành công", MsgBoxStyle.OkOnly)

                Dim LopHoc As New LopHocBUS
                Dim sSo As Integer
                sSo = LopHoc.LaySiSo(maLH)
                Me.txtSiSo.Text = sSo

                Dim HocSinh As New HocSinhBUS
                Dim LHChuaXep As New DataTable
                LHChuaXep = HocSinh.DSHSChuaXep()
                Me.gridDSHocSinh.DataSource = LHChuaXep

                Dim bang As DataTable
                bang = dataBus.Doc_Cau_Truc("HocSinh")
                Me.gridXepLop.DataSource = bang

            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub gridXepLop_CellMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles gridXepLop.CellMouseClick
        Dim index As Integer
        index = Me.gridXepLop.CurrentRow.Index
        bangHS.Rows.RemoveAt(index)
        Dim i As Integer
        For i = 0 To gridXepLop.Rows.Count - 2
            Me.gridXepLop.Rows(i).Cells(0).Value = i + 1
        Next
    End Sub
End Class
